﻿/****************************** Module Header ******************************\
 * Module Name:  JMarqueer.cs
 * Module Version:  1.0.0 (2012-10-18)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This componse is use for JMarqueer.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace UCF.Core.Web.CustomControls
{
    /// <summary>
    /// 无缝滚动插件
    /// </summary>
    /// <example>
    /// <![CDATA[
    /// <so:JMarqueer runat="server" ID="tt"><img src="http://js.alixixi.com/img/201108/1.jpg"><img src="http://js.alixixi.com/img/201108/1.jpg"><img src="http://js.alixixi.com/img/201108/1.jpg"><img src="http://js.alixixi.com/img/201108/1.jpg"><img src="http://js.alixixi.com/img/201108/1.jpg"></so:JMarqueer>
    /// ]]>
    /// </example>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:Marqueer runat=server></{0}:Marqueer>")]
    public class JMarqueer : HtmlContainerControl
    {
        /// <summary>
        /// 滚动方向
        /// <para>值为：Left,Right,Up,Down</para>
        /// </summary>
        public string Direction { get; set; }

        /// <summary>
        /// 获取或设置宽度
        /// </summary>
        public int Width { get; set; }

        /// <summary>
        /// 获取或设置高度
        /// </summary>
        public int Height { get; set; }
        protected override void OnPreRender(EventArgs e)
        {
            
            Page.PreRenderComplete += Page_PreRenderComplete;
            base.OnPreRender(e);
        }
        private void Page_PreRenderComplete(object s, EventArgs e)
        {
            switch (Direction)
            {
                case "Up":
                    if (Width <= 0) Width = 410;
                    if (Height <= 0) Height = 253;
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "MarqueeUp", "function MarqueeUp(c,c1,c2){if(c2.offsetTop-c.scrollTop<=0){c.scrollTop-=c1.offsetHeight;}else{c.scrollTop++}};", true);
                    Page.ClientScript.RegisterStartupScript(typeof(Page), this.UniqueID, string.Format("$('#{0}').children().wrapAll('<div style=\"overflow:hidden;height:{2}px;width:{1}px;\"><div></div><div></div></div>').wrap('<p />');var mqUp{0}={{}};mqUp{0}.$o=$('#{0}').children(':eq(0)');mqUp{0}.c2=mqUp{0}.$o.children(':eq(1)')[0];mqUp{0}.c1=mqUp{0}.$o.children(':eq(0)')[0];mqUp{0}.c=mqUp{0}.$o[0];mqUp{0}.c2.innerHTML=mqUp{0}.c1.innerHTML;mqUp{0}.ti=setInterval('MarqueeUp(mqUp{0}.c,mqUp{0}.c1,mqUp{0}.c2)',30);mqUp{0}.c.onmouseover=function(){{clearInterval(mqUp{0}.ti)}};mqUp{0}.c.onmouseout=function(){{mqUp{0}.ti=setInterval('MarqueeUp(mqUp{0}.c, mqUp{0}.c1, mqUp{0}.c2)',30)}};", this.UniqueID,Width,Height), true);
                    break;
                case "Down":
                    if (Width <= 0) Width = 410;
                    if (Height <= 0) Height = 253;
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "MarqueeDown", "function MarqueeDown(c,c1,c2){if(c1.offsetTop-c.scrollTop>=0){c.scrollTop+=c2.offsetHeight}else{c.scrollTop--}};", true);
                    Page.ClientScript.RegisterStartupScript(typeof(Page), this.UniqueID, string.Format("var mqDown{0}={{}};$('#{0}').children().wrapAll('<div style=\"overflow:hidden;height:{2}px;width:{1}px;\"><div></div><div></div></div>').wrap('<p />');mqDown{0}.$o=$('#{0}').children(':eq(0)');mqDown{0}.c2=mqDown{0}.$o.children(':eq(1)')[0];mqDown{0}.c1=mqDown{0}.$o.children(':eq(0)')[0];mqDown{0}.c=mqDown{0}.$o[0];mqDown{0}.c2.innerHTML=mqDown{0}.c1.innerHTML;mqDown{0}.c.scrollTop=mqDown{0}.c.scrollHeight;mqDown{0}.ti=setInterval('MarqueeDown(mqDown{0}.c,mqDown{0}.c1,mqDown{0}.c2)',30);mqDown{0}.c.onmouseover=function(){{clearInterval(mqDown{0}.ti)}};mqDown{0}.c.onmouseout=function(){{mqDown{0}.ti=setInterval('MarqueeDown(mqDown{0}.c,mqDown{0}.c1,mqDown{0}.c2)',30)}};", this.UniqueID,Width,Height), true);
                    break;
                case "Right":
                    if (Width <= 0) Width = 500;
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "MarqueeRight", "function MarqueeRight(c,c1,c2){if(c.scrollLeft<=0){c.scrollLeft+=c2.offsetWidth}else{c.scrollLeft--}};", true);
                    Page.ClientScript.RegisterStartupScript(typeof(Page), this.UniqueID, string.Format("$('#{0}').children().wrapAll('<div style=\"overflow:hidden;width:{1}px;\"><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td class=\"c1\" valign=\"top\" align=\"center\"><table cellpadding=\"2\" cellspacing=\"0\" border=\"0\"><tr align=\"center\"></tr></table></td><td class=\"c2\" valign=\"top\"></td></tr></table></div>').wrap('<td><p /></td>');var mqRight{0}={{}};mqRight{0}.$o=$('#{0}');mqRight{0}.c1=mqRight{0}.$o.children().children().children().children().children(':eq(0)')[0];mqRight{0}.c2=$(mqRight{0}.c1).next()[0];mqRight{0}.c=mqRight{0}.$o.children()[0];mqRight{0}.c2.innerHTML=mqRight{0}.c1.innerHTML;mqRight{0}.ti=setInterval('MarqueeRight(mqRight{0}.c,mqRight{0}.c1,mqRight{0}.c2)',30);mqRight{0}.c.onmouseover=function(){{clearInterval(mqRight{0}.ti)}};mqRight{0}.c.onmouseout=function(){{mqRight{0}.ti=setInterval('MarqueeRight(mqRight{0}.c,mqRight{0}.c1,mqRight{0}.c2)',30)}};", this.UniqueID, Width), true);
                    break;
                default:
                    if (Width <= 0) Width = 500;
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "MarqueeLeft", "function MarqueeLeft(c,c1,c2){if(c2.offsetWidth-c.scrollLeft<=0){c.scrollLeft-=c1.offsetWidth}else{c.scrollLeft++}};", true);
                    Page.ClientScript.RegisterStartupScript(typeof(Page), this.UniqueID, string.Format("$('#{0}').children().wrapAll('<div style=\"overflow:hidden;width:{1}px;\"><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td valign=\"top\" align=\"center\"><table cellpadding=\"2\" cellspacing=\"0\" border=\"0\"><tr align=\"center\"></tr></table></td><td valign=\"top\"></td></tr></table></div>').wrap('<td><p /></td>');var mqLeft{0}={{}};mqLeft{0}.$o=$('#{0}');mqLeft{0}.c1=mqLeft{0}.$o.children().children().children().children().children(':eq(0)')[0];mqLeft{0}.c2=$(mqLeft{0}.c1).next()[0];mqLeft{0}.c=mqLeft{0}.$o.children()[0];mqLeft{0}.c2.innerHTML=mqLeft{0}.c1.innerHTML;mqLeft{0}.ti=setInterval('MarqueeLeft(mqLeft{0}.c,mqLeft{0}.c1,mqLeft{0}.c2)',30);mqLeft{0}.c.onmouseover=function(){{clearInterval(mqLeft{0}.ti)}};mqLeft{0}.c.onmouseout=function(){{mqLeft{0}.ti=setInterval('MarqueeLeft(mqLeft{0}.c,mqLeft{0}.c1,mqLeft{0}.c2)',30)}};", this.UniqueID,Width), true);
                    break;
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(string.Format("<div id='{0}'>", this.UniqueID));
            base.RenderChildren(writer);
            writer.Write("</div>");
        }
    }
}
